let app = getApp();
Page({
    data: {
         loginImg: "/public/icon/luockoo_logo.png",
         checkIcon: "/public/icon/select_gray.png",
         checkedIcon: "/public/icon/check_on.png",
         selectFlag: false,
         userNumber: "",
         password: ""
    },
    selectFn: function () {
        let selectFlag = !this.data.selectFlag;
        this.setData({
            selectFlag: selectFlag
        })
    
    },
    showErrorFn: function (errorMsg, iconStatus) {
        wx.showToast({
          title: errorMsg,
          icon: iconStatus || "error",
          mask: true
      
        })
    },
    titleFn: function () {
      let _this = this;
      wx.showModal({
          content: '我已阅读并同意《珞珂家居平台商户入驻协议》《信息数据收集协议》',
          cancelText: "不同意",
          confirmText: "同意",
          success (res) {
            if (res.confirm) {        
                _this.setData({
                    selectFlag: true
                }, function () {
                  _this.loginFn()
                })
             

              } else if (res.cancel) {
                console.log('用户点击取消')
            }
          }
        })
    },
    loginFn: function () {
        let _this = this;
        let selectFlag = this.data.selectFlag;
        let storeId =  1;
        let storeType = 6;
        let userNumber = this.data.userNumber;
        let password = this.data.password;
        let reg = /^[1][3456789][0-9]{9}$/;
        // const regex = /^1[3456789]\d{9}$/; // 手机号正则表达式
        if (!reg.test(userNumber)) {

            _this.showErrorFn("请输入正确手机号", "none")
            return ;
        }
        if (!password) {
            _this.showErrorFn("请输入密码") 
            return ;
        }
        if (!selectFlag) {
            _this.titleFn() 
            return ;
        }
        wx.showLoading({
          title: '加载中',
        })
          wx.request({
            url: app.globalData.url, 
            method: "get",
            data: {
                api:'app.login.login',
                storeId: 1,
                storeType: 6,
                phone: userNumber,
                password: password
            },
            header: {
              'content-type': 'application/json' 
            },
            success (res) {
                if (res.data && res.data.code == 200) {
                    let resData = res.data.data;
                    let loginInfo = {
                        userNumber: userNumber,
                        password: password
                    }
                    if (resData.userType == 1) {
                        wx.hideLoading()
                        wx.showModal({
                            title: "提示",
                            content: "您是个人用户,请用手机号快捷登录",
                            success (res) {
                                if (res.confirm) {
                                    wx.navigateBack()
                                }
                            }
                        })
                        return ;
                    }
                    wx.setStorage({
                        key: "loginInfo",
                        data: loginInfo
                    })
                    setTimeout(()=> {          
                        wx.setStorage({
                            key: "userInfo",
                            data: resData
                        })
                        wx.hideLoading()
                        wx.switchTab({
                            url: '/pages/index/index',
                        })

                    }, 2000)
                } else {
                  wx.hideLoading()
                  
                  _this.showErrorFn(res.data.message, "none")
                  if (res.data.code == "50066") {
                      setTimeout(()=>{

                        wx.navigateTo({
                          url: '/pages/register/register',
                        })
                      },2000)
                  }
                }
            }
          })
    },
    numberFn: function (e) {
          let userNumber = e.detail.value;
          this.setData({
              userNumber: userNumber
          })
    },


    passwordFn: function (e) {
          let password = e.detail.value;
          this.setData({        
              password: password
          })
    }
})